<!DOCTYPE html>
<html lang="zh-CN">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.png">
  <link rel="icon" type="image/png" href="/blog/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#35495e">
  <meta name="description" content="小菜头的博客，主要记录学习笔记，包含：html,css,javascript,jQuery,vue等">
  <meta name="author" content="A-cai">
  <meta name="keywords" content="html,css,javascript,jQuery,vue">
  <title>CSS实现多行文本溢出省略 - A-cai的blog</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/blog/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/atom-one-dark.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/blog/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/blog/js/utils.js" ></script>
<meta name="generator" content="Hexo 4.2.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/blog/">&nbsp;<strong>A-cai的学习笔记</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/blog/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-06-26 14:05">
      2020年6月26日 下午
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.3k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      28
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
              <p class="note note-info">
                
                  本文最后更新于：2020年3月15日 晚上
                
              </p>
            
            <article class="markdown-body">
              <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>多行文本超出高度限制出现省略号 , 移动端多为webkit内核的 , 有扩展属性-webkit-line-clamp , 但并不是CSS规范中的属性 , PC端往往要借助js去实现这一效果，但麻烦且不是很靠谱，今天就用纯CSS来实现一个完全兼容的多行省略。</p>
<h1 id="正文"><a href="#正文" class="headerlink" title="正文"></a>正文</h1><h2 id="一、webkit内核的实现"><a href="#一、webkit内核的实现" class="headerlink" title="一、webkit内核的实现"></a>一、webkit内核的实现</h2><p><code>-webkit-line-clamp</code>设置块元素包含的文本行数；<br><code>display: -webkit-box</code>设置块元素的布局为伸缩布局；<br><code>-webkit-box-orient</code>设置伸缩项的布局方向；<br><code>text-overflow: ellipsis;</code>则表示超出盒子的部分使用省略号表示。</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">style</span> = <span class="hljs-string">'width:400px;</span></span>
<span class="hljs-tag"><span class="hljs-string">              height:70px;</span></span>
<span class="hljs-tag"><span class="hljs-string">              border:1px solid red;'</span>&gt;</span>
           <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">'display:</span></span>
<span class="hljs-tag"><span class="hljs-string">                     -webkit-box;//对象作为弹性伸缩盒子模型显示 </span></span>
<span class="hljs-tag"><span class="hljs-string">                     -webkit-box-orient: vertical;//设置或检索伸缩盒对象的子元素的排列方式 </span></span>
<span class="hljs-tag"><span class="hljs-string">                     -webkit-line-clamp: 2;//溢出省略的界限</span></span>
<span class="hljs-tag"><span class="hljs-string">                     overflow:hidden;//设置隐藏溢出元素'</span>&gt;</span>
                 这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本这是一些文本
         <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>



<h2 id="二、非webkit内核实现"><a href="#二、非webkit内核实现" class="headerlink" title="二、非webkit内核实现"></a>二、非webkit内核实现</h2><blockquote>
<p>不过本文将要介绍的方法是<code>采用CSS规范中的属性</code>，并结合特殊的实现技巧完成的。这意味着在实现CSS2.1规范的浏览器中都是可以兼容的，不将仅仅是纯粹的移动端领域，在传统的PC浏览器（你们懂得我指的是哪些浏览器）中仍是可行的。好吧，让我们一起见识下。</p>
</blockquote>
<p>一共7个步骤，最简单的就是截断文本，最难的部分则是<code>让一个元素出在其父块的溢出时的右下方</code>，并且当父元素未溢出时该元素消失不可见。（<code>代码可直接粘贴看效果</code>）</p>
<h3 id="1、基本结构布局"><a href="#1、基本结构布局" class="headerlink" title="1、基本结构布局"></a>1、基本结构布局</h3><p>最简单的开始：当父包含框比较小时，将子元素布局到父包含框的右下角；<br>HTML</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wrap"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"prop"</span>&gt;</span>
        1.prop
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main"</span>&gt;</span>
        2.main 这里是主题内容，多行省略
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"end"</span>&gt;</span>
        3.end
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>CSS</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.wrap</span> &#123;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">20px</span> <span class="hljs-number">50px</span> <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid <span class="hljs-number">#AAA</span>;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">25px</span>;
&#125;
<span class="hljs-selector-class">.prop</span> &#123;
    <span class="hljs-attribute">float</span>: left;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAF</span>;
&#125;
<span class="hljs-selector-class">.main</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">350px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#AFF</span>;
    <span class="hljs-attribute">word-break</span>: break-all;
&#125;
<span class="hljs-selector-class">.end</span> &#123;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FFA</span>;
&#125;</code></pre></div>

<p><strong>效果如下面几张图（<code>注意黄色end的位置</code>）, 其实这个实现完全利用了元素浮动的基本规则。</strong></p>
<p><img src="https://segmentfault.com/img/bVLA53?w=417&h=69" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<p><img src="https://segmentfault.com/img/bVLA6t?w=426&h=122" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<h3 id="2、定位模拟’…’"><a href="#2、定位模拟’…’" class="headerlink" title="2、定位模拟’…’"></a>2、定位模拟’…’</h3><p>我们通过创建一个子元素来替代将要显示的省略号，当本文溢出的情形下该元素显示在正确的位置上。在接下来的实现中，我们创建一个realend元素，利用上一步骤中end元素浮动后的位置来实现realEnd元素的定位。<br>HTML</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wrap"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"prop"</span>&gt;</span>1.prop<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main"</span>&gt;</span>
        2.main 这里是要多行文本溢出省略的，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"end"</span>&gt;</span>
        3.end
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"realEnd"</span>&gt;</span>realEnd<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>CSS</p>
<div class="hljs"><pre><code class="hljs css">// 新增下面样式即可
<span class="hljs-selector-class">.realEnd</span> &#123;
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">25px</span>; <span class="hljs-comment">/*等于高度的负值，就是文字的行高*/</span>
    <span class="hljs-attribute">left</span>: <span class="hljs-number">350px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAA</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
&#125;</code></pre></div>

<p><strong>效果如图下图</strong></p>
<p><img src="https://segmentfault.com/img/bVLBbo?w=431&h=119" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<p>若父元素并没有溢出，那么realend元素会出现在其右侧(设置wrap overflow：hidden即可，下面会解决)</p>
<p><img src="https://segmentfault.com/img/bVLBbO?w=792&h=133" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<h3 id="3、优化定位"><a href="#3、优化定位" class="headerlink" title="3、优化定位"></a>3、优化定位</h3><p>第二步中我们针对end元素设置了相对定位，对realEnd元素设置了绝对定位。但是我们可以采用更简单的代码来实现，即只使用相对定位。熟悉定位模型的同学应该知道，相对定位的元素仍然占据文本流，同时针对元素设置偏移。这样， 就可以去掉end元素了，仅针对realEnd元素设置相对定位。<br>HTML</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wrap"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"prop"</span>&gt;</span>1.prop<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main"</span>&gt;</span>
        2.main 这里是要多行文本溢出省略的，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"realEnd"</span>&gt;</span>realEnd<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>CSS</p>
<div class="hljs"><pre><code class="hljs css">// 删除end样式，realEnd样式更改如下
<span class="hljs-selector-class">.realEnd</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">25px</span>; <span class="hljs-comment">/*等于高度的负值，就是文字的行高*/</span>
    <span class="hljs-attribute">left</span>: <span class="hljs-number">350px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAA</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
&#125;</code></pre></div>

<p><strong>效果如图（<code>黄色的end已经没了</code>）</strong></p>
<p><img src="https://segmentfault.com/img/bVLBee?w=457&h=130" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<h3 id="4、削窄prop元素"><a href="#4、削窄prop元素" class="headerlink" title="4、削窄prop元素"></a>4、削窄prop元素</h3><p>目前，最左侧的prop元素的作用在于让realend元素在文本溢出时处在其正下方，在前几节的示例代码中为了直观的演示，设置prop元素的宽度为50px，那么现在为了更好的模拟实际的效果，我们缩小prop元素的宽度。</p>
<p>CSS</p>
<div class="hljs"><pre><code class="hljs css">// 更改部分样式如下
<span class="hljs-selector-class">.prop</span> &#123;
    <span class="hljs-attribute">float</span>: left;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">5px</span>;<span class="hljs-comment">/*缩小宽度为5px，其余属性不变*/</span>
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAF</span>;
&#125;
<span class="hljs-selector-class">.main</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">5px</span>;<span class="hljs-comment">/*让main元素左移5px，这样main元素在宽度上就完全占满了父元素；*/</span>
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#AFF</span>;
    <span class="hljs-attribute">word-break</span>: break-all;
&#125;
<span class="hljs-selector-class">.realEnd</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">25px</span>; <span class="hljs-comment">/*等于高度的负值，就是文字的行高*/</span>
    <span class="hljs-attribute">left</span>: <span class="hljs-number">400px</span>;
    <span class="hljs-comment">/*而设置margin-left: -50px、padding-right: 5px则是为了让realend元素的盒模型的最终宽度计算为5px。*/</span>
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">50px</span>;
    <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAA</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
&#125;</code></pre></div>

<blockquote>
<p>由于CSS规范规定padding的值不可以为负数，因此只有设置margind-left为负值，<code>且等于其宽度</code>。这样做的最终目的就是保证realend元素处在prop元素的下方，保证在文本溢出的情况下定位准确性:</p>
</blockquote>
<p><strong>效果图如下</strong></p>
<p><img src="https://segmentfault.com/img/bVLBlq?w=423&h=122" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<h3 id="5、流式布局-伪元素"><a href="#5、流式布局-伪元素" class="headerlink" title="5、流式布局 + 伪元素"></a>5、流式布局 + 伪元素</h3><p>目前，realend元素的相关属性仍采用px度量，为了更好的扩展性，可以改用%替代。</p>
<p>同时，prop元素和realend元素可以采用伪元素来实现，减少额外标签的使用。</p>
<p>CSS</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.mutil-line-ellipsis</span> &#123;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">20px</span> <span class="hljs-number">50px</span> <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid <span class="hljs-number">#AAA</span>;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-comment">/*overflow: hidden;*/</span>
&#125;
<span class="hljs-comment">/*相当于之前的prop*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span><span class="hljs-selector-pseudo">:before</span> &#123;
    <span class="hljs-attribute">content</span>: <span class="hljs-string">''</span>;
    <span class="hljs-attribute">float</span>: left;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">5px</span>;<span class="hljs-comment">/*缩小宽度为5px，其余属性不变*/</span>
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAF</span>;
&#125;
<span class="hljs-comment">/*相当于之前的main*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span> &gt; <span class="hljs-selector-pseudo">:first-child</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">5px</span>;<span class="hljs-comment">/*让main元素左移5px，这样main元素在宽度上就完全占满了父元素；*/</span>
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#AFF</span>;
    <span class="hljs-attribute">word-break</span>: break-all;
&#125;
<span class="hljs-comment">/*相当于之前的realEnd*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span><span class="hljs-selector-pseudo">:after</span> &#123;
    <span class="hljs-attribute">content</span>: <span class="hljs-string">'realEnd'</span>;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">25px</span>; <span class="hljs-comment">/*等于高度的负值，就是文字的行高*/</span>
    <span class="hljs-attribute">left</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-comment">/*而设置margin-left: -50px、padding-right: 5px则是为了让realend元素的盒模型的最终宽度计算为5px。*/</span>
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">50px</span>;
    <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#FAA</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
&#125;</code></pre></div>

<p>HTML</p>
<div class="hljs"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mutil-line-ellipsis"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        2.main 这里是要多行文本溢出省略的，内容多一点，内容多一点，内容多一点，内容多一点
        内容多一点，内容多一点，内容多一点，内容多一点，内容多一点，内容多一点
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></code></pre></div>

<p>效果和上一步一样</p>
<h3 id="6、隐藏"><a href="#6、隐藏" class="headerlink" title="6、隐藏"></a>6、隐藏</h3><p>之前的实现中在文本未溢出的情况下，realEnd元素会出现在父元素的右侧，正如<br><img src="https://segmentfault.com/img/bVLHXc?w=882&h=105" srcset="/blog/img/loading.gif" alt="clipboard.png"><br>设置<br>CSS</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.mutil-line-ellipsis</span> &#123;
     <span class="hljs-attribute">overflow</span>: hidden;   
&#125;</code></pre></div>

<h3 id="7、美化"><a href="#7、美化" class="headerlink" title="7、美化"></a>7、美化</h3><p>现在我们离完结就差一步了，即去掉各元素的背景色，并且用“…”替换文本。最后为了优化体验，采用渐变来隐藏“…”覆盖的文本，（省略了一些兼容性的属性）。<br>CSS</p>
<div class="hljs"><pre><code class="hljs css"><span class="hljs-selector-class">.mutil-line-ellipsis</span> &#123;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">20px</span> <span class="hljs-number">50px</span> <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid <span class="hljs-number">#AAA</span>;
    <span class="hljs-attribute">line-height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">overflow</span>: hidden;
&#125;
<span class="hljs-comment">/*相当于之前的prop*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span><span class="hljs-selector-pseudo">:before</span> &#123;
    <span class="hljs-attribute">content</span>: <span class="hljs-string">''</span>;
    <span class="hljs-attribute">float</span>: left;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">5px</span>;<span class="hljs-comment">/*缩小宽度为5px，其余属性不变*/</span>
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50px</span>;
&#125;
<span class="hljs-comment">/*相当于之前的main*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span> &gt; <span class="hljs-selector-pseudo">:first-child</span> &#123;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">5px</span>;<span class="hljs-comment">/*让main元素左移5px，这样main元素在宽度上就完全占满了父元素；*/</span>
    <span class="hljs-attribute">word-break</span>: break-all;
&#125;
<span class="hljs-comment">/*相当于之前的realEnd*/</span>
<span class="hljs-selector-class">.mutil-line-ellipsis</span><span class="hljs-selector-pseudo">:after</span> &#123;
    <span class="hljs-attribute">content</span>: <span class="hljs-string">'...'</span>;
    <span class="hljs-attribute">box-sizing</span>: content-box;
    <span class="hljs-attribute">float</span>: right;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">25px</span>; <span class="hljs-comment">/*等于高度的负值，就是文字的行高*/</span>
    <span class="hljs-attribute">left</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-comment">/*而设置margin-left: -50px、padding-right: 5px则是为了让realend元素的盒模型的最终宽度计算为5px。*/</span>
    <span class="hljs-attribute">margin-left</span>: -<span class="hljs-number">50px</span>;
    <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
    <span class="hljs-attribute">text-align</span>: right;
    <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(to right, rgba(<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0</span>), <span class="hljs-number">#ffffff</span> <span class="hljs-number">40px</span>);
&#125;</code></pre></div>

<p>效果（自己可调整省略样式）</p>
<p><img src="https://segmentfault.com/img/bVLHZu?w=434&h=79" srcset="/blog/img/loading.gif" alt="clipboard.png"></p>
<h1 id="总结之兼容性"><a href="#总结之兼容性" class="headerlink" title="总结之兼容性"></a>总结之兼容性</h1><p>从上文的实现细节来看，我们利用的技巧完全是CSS规范中的浮动+定位+盒模型宽度计算，唯一存在兼容性问题的在于无关痛痒的渐变实现，因此可以在大多数浏览器下进行尝试。</p>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/blog/2020/06/26/%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E8%A7%84%E8%8C%83/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">前端开发规范</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/blog/2020/06/26/BFC%E3%80%81IFC%E3%80%81GFC%E5%92%8CFFC/">
                        <span class="hidden-mobile">BFC、IFC、GFC和FFC</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                

              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    

    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/debouncer.js" ></script>
<script  src="/blog/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/blog/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/blog/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "CSS实现多行文本溢出省略&nbsp;",
      ],
      cursorChar: "|",
      typeSpeed: 70,
      loop: true,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/blog/js/local-search.js" ></script>
  <script>
    var path = "/blog/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  













</body>
</html>
